<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发布试卷</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/TeacherAll.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
</head>
<body>
<h1 class="UpLoadTopBtn">试卷编辑</h1>
<div class="CaseMesDiv" id="caseMesDiv">
    <input class="deleteTopIcBtn" id="deleteTopicBtn" onclick="checkbox()" type="button" value="删除所选题目">
    <input class="UpLoadTopIcBtn" id="UpLoadTopIcBtn" type="button" onclick="SelTopIcKu()" value="从题库中加入">
    <a href="${pageContext.request.contextPath}/GoBhSy/openWeb?findU=UpLoadExcel.jsp">
        <input class="UpLoadExcelBtn" type="button" value="通过Excel表格加入">
    </a>
</div>
<div class="FileDiv UpLoadCaseTcDiv" id="UpLoadCaseTcDiv">

</div>
<form action="/BhSyCase/BhSyDao/Teacher/pushTheCase" method="post">
<div class="UpLoadScTopIc" id="uploadScTopIc">
    <%--   判断题目集合不为空   --%>
    <c:set var="num" value="1"></c:set>
    <c:if test="${bhTopList!=null&&flagUlCase==1}">
        <c:forEach var="j" items="${bhTopList}">
            <div class="UpLoadTopIcDiv">
                <input class="cbUpLoadTic" type="checkbox" name="topCb" value="${j.cPor}"><p class="CaseTopTmFont">${num}. ${j.cPor}----${j.cType}</p>
                <label>分数:</label><input class="caseNum" type="text" name="caseNum" value="${j.cNum}">
                <c:if test="${j.cA!=null}">
                    <br>
                    <p class="CaseTopIcA TopIcXz">A :${j.cA}</p>
                </c:if>
                <c:if test="${j.cB!=null}">
                    <br>
                    <p class="CaseTopIcB TopIcXz">B :${j.cB}</p>
                </c:if>
                <c:if test="${j.cC!=null}">
                    <br>
                    <p class="CaseTopIcC TopIcXz">C: ${j.cC}</p>
                </c:if>
                <c:if test="${j.cD!=null}">
                    <br>
                    <p class="CaseTopIcC TopIcXz">C: ${j.cD}</p>
                </c:if>
            </div>
            <c:set var="num" value="${num=num+1}"></c:set>
        </c:forEach>
    </c:if>
</div>
    <input class="pushCase" type="submit" value="发布试卷">
</form>
</body>
<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script>
    var http = new XMLHttpRequest();
    function checkbox()
    {
        var chestr="";
        var str=document.getElementsByName("topCb");

        for (let i=0;i<str.length;i++)
        {
            if(str[i].checked == true)
            {
                chestr+=str[i].value+"/";
            }
        }
        if(chestr == "")
        {
            alert("请先选择复选框～！");
            return false;
        }
        else
        {
            deleteTopic(chestr);
            return true;
        }
    }

    function SelTopIcKu(){
        var upLoadCaseTcDiv = document.getElementById("UpLoadCaseTcDiv");
        upLoadCaseTcDiv.style.display="block";
        http.open("get","${pageContext.request.contextPath}/BhSyDao/Teacher/ajaxSelTioIcKu");
        http.send();
        http.onreadystatechange=function () {
            if (http.readyState == 4 && http.status == 200) {
                var uTopicKu = http.responseText;
                var jsonTicKu = JSON.parse(uTopicKu);

                for (let i = 0; i < jsonTicKu.length ; i++) {
                    var theFile = document.createElement("div");
                    theFile.className="theFile";
                    theFile.style.cursor="pointer";
                    var fileImg = document.createElement("img");
                    fileImg.className="fileImg";
                    fileImg.src="${pageContext.request.contextPath}/img/BhCase/fileimg.png";
                    theFile.append(fileImg);
                    var fileFont = document.createElement("p");
                    fileFont.className="fileFont";
                    fileFont.innerText=jsonTicKu[i].tName;
                    theFile.append(fileFont);
                    upLoadCaseTcDiv.append(theFile);
                }
                var theFile = document.getElementsByClassName("theFile");
                for (let i = 0; i < theFile.length ; i++) {
                    theFile[i].onclick = function (){
                        http.open("get","${pageContext.request.contextPath}/BhSyDao/Teacher/ajaxSelTopIc?tName="+jsonTicKu[i].tName);
                        http.send();
                        http.onreadystatechange=function () {
                            if (http.readyState == 4 && http.status == 200) {
                                var uTopic = http.responseText;
                                var jsonTic = JSON.parse(uTopic);
                                console.log(jsonTic)
                                upLoadCaseTcDiv.innerHTML="";
                                var bhTopicTheCt = document.createElement("div");
                                bhTopicTheCt.className="bhTopicTheCt";
                                upLoadCaseTcDiv.append(bhTopicTheCt);
                                var bhTopicBtnTj = document.createElement("input");
                                bhTopicBtnTj.type="button";
                                bhTopicBtnTj.value="上传题目";
                                bhTopicBtnTj.id="bhTopicBtnTj";

                                bhTopicBtnTj.className="bhTopicBtnTj";
                                upLoadCaseTcDiv.append(bhTopicBtnTj);
                                var bhTopicBtnQx = document.createElement("input");
                                bhTopicBtnQx.type="button";
                                bhTopicBtnQx.value="取消上传";
                                bhTopicBtnQx.style.cursor="pointer";
                                bhTopicBtnQx.className="bhTopicBtnQx";
                                bhTopicBtnQx.id="bhTopicBtnQx";
                                upLoadCaseTcDiv.append(bhTopicBtnQx);
                                for (let i = 0; i < jsonTic.length ; i++) {
                                    var bhTopicMDiv = document.createElement("div");
                                    bhTopicMDiv.className="bhTopicMDiv";
                                    bhTopicTheCt.append(bhTopicMDiv);
                                    var bhTopicCheckbox = document.createElement("input");
                                    bhTopicCheckbox.type="checkbox";
                                    bhTopicCheckbox.name="topIName";
                                    bhTopicCheckbox.value=jsonTic[i].cPor;
                                    bhTopicMDiv.append(bhTopicCheckbox);
                                    var bhTopicTPro = document.createElement("p");
                                    bhTopicTPro.className="bhTopicTPro";
                                    bhTopicTPro.innerText=jsonTic[i].cPor;
                                    bhTopicMDiv.append(bhTopicTPro);
                                }
                                var bhTopicBtnTj = document.getElementById("bhTopicBtnTj");
                                bhTopicBtnTj.onclick = function (){
                                    theKuAddTopIc();
                                }
                                var bhTopicBtnQx = document.getElementById("bhTopicBtnQx");
                                bhTopicBtnQx.onclick = function (){
                                    upLoadCaseTcDiv.style.display="none";
                                    upLoadCaseTcDiv.innerHTML=""
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    function theKuAddTopIc(){
        var topIName = document.getElementsByName("topIName");
        var topName = "";
        for (let i = 0; i < topIName.length ; i++) {
            if(topIName[i].checked == true)
            {
                topName+=topIName[i].value+"/";
            }
        }
        if(topName == "")
        {
            alert("请先选择题目～！");
            return false;
        }
        else
        {
            theKuAddTopIcHttp(topName);
            return true;
        }
    }
    function theKuAddTopIcHttp(topName){
        var getTop = topName;
        getTop = getTop.replace(/\+/g, "%2B");
        getTop = getTop.replace(/\&/g, "%26");
        alert("启动ajax")
        var upLoadCaseTcDiv = document.getElementById("UpLoadCaseTcDiv");
        upLoadCaseTcDiv.innerHTML=""
        upLoadCaseTcDiv.style.display="none";
        var uploadScTopIc = document.getElementById("uploadScTopIc");
        http.open("post","${pageContext.request.contextPath}/BhSyDao/Teacher/ajaxTheTopicKuAddTopic");
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.send("uTopic="+getTop);
        http.onreadystatechange=function (){
            if(http.readyState==4 && http.status==200){
                uploadScTopIc.innerHTML="";
                var uKuTopic = http.responseText;
                var jsonKuTopic = JSON.parse(uKuTopic);
                console.log(jsonKuTopic)
                addTopIcDiv(jsonKuTopic);
            }
        }

    }
    function deleteTopic(TheTopic){
        var getTop = TheTopic;
        getTop = getTop.replace(/\+/g, "%2B");
        getTop = getTop.replace(/\&/g, "%26");
        http.open("get","${pageContext.request.contextPath}/BhSyDao/Teacher/ajaxDelTopic?uTopic="+getTop,true);
        http.send();
        http.onreadystatechange=function (){
            if(http.readyState==4 && http.status==200){
                var uTopic = http.responseText;
                var jsonTic = JSON.parse(uTopic);
                console.log(jsonTic);
                addTopIcDiv(jsonTic);
            }
        }
    }
    function addTopIcDiv(AllTopic){
        var uploadScTopIc = document.getElementById("uploadScTopIc");
        uploadScTopIc.innerHTML="";
        for (let i = 0; i < AllTopic.length ; i++) {
            //题目布局div
            var upLoadTopIcDiv = document.createElement("div");
            upLoadTopIcDiv.className="UpLoadTopIcDiv";

            //题目标题
            var cbUpLoadTic = document.createElement("input");
            cbUpLoadTic.className="cbUpLoadTic";
            cbUpLoadTic.type="checkbox";
            cbUpLoadTic.name="topCb";
            cbUpLoadTic.value=AllTopic[i].cPor.replace("\n","");
            upLoadTopIcDiv.append(cbUpLoadTic);

            //题目问题
            var caseTopTmFont = document.createElement("p");
            caseTopTmFont.className="CaseTopTmFont";
            caseTopTmFont.innerText=(i+1)+". "+AllTopic[i].cPor.replace("\n","")+"----"+AllTopic[i].cType.replace("\n","");
            upLoadTopIcDiv.append(caseTopTmFont);
            //分数标签
            var labNum = document.createElement("label");
            labNum.innerText="分数";
            //题目分数
            var caseNum = document.createElement("input");
            caseNum.className="caseNum";
            caseNum.name="caseNum";
            caseNum.value=AllTopic[i].cNum;
            upLoadTopIcDiv.append(caseNum);
            //换行
            var br01 = document.createElement("br");
            var br02 = document.createElement("br");
            var br03 = document.createElement("br");
            var br04 = document.createElement("br");
            console.log(AllTopic[i].cA)
            if (AllTopic[i].cType=="多选题"||AllTopic[i].cType=="单选题"||AllTopic[i].cType=="单选"||AllTopic[i].cType=="多选"){
                //题目A
                if (AllTopic[i].cA!=null||AllTopic[i].cA!=""){
                    var caseTopIcA = document.createElement("p");
                    caseTopIcA.className = "CaseTopIcA TopIcXz";
                    caseTopIcA.innerText="A: "+AllTopic[i].cA;
                    console.log(AllTopic[i].cA)
                    upLoadTopIcDiv.append(br01);
                    upLoadTopIcDiv.append(caseTopIcA);
                }
                if (AllTopic[i].cB!=null||AllTopic[i].cB!=""){
                    //题目B
                    var caseTopIcB = document.createElement("p");
                    caseTopIcB.className = "CaseTopIcB TopIcXz";
                    caseTopIcB.innerText="B: "+AllTopic[i].cB;
                    upLoadTopIcDiv.append(br02);
                    upLoadTopIcDiv.append(caseTopIcB);
                }
                if (AllTopic[i].cC!=null||AllTopic[i].cC!=""){
                    //题目C
                    var caseTopIcC = document.createElement("p");
                    caseTopIcC.className = "CaseTopIcc TopIcXz";
                    caseTopIcC.innerText="C: "+AllTopic[i].cC;
                    upLoadTopIcDiv.append(br03);
                    upLoadTopIcDiv.append(caseTopIcC);
                }
                if (AllTopic[i].cD!=null||AllTopic[i].cD!=""){
                    //题目D
                    var caseTopIcD = document.createElement("p");
                    caseTopIcD.className = "CaseTopIcD TopIcXz";
                    caseTopIcD.innerText="D: "+AllTopic[i].cD;
                    upLoadTopIcDiv.append(br04);
                    upLoadTopIcDiv.append(caseTopIcD);
                }
            }
            uploadScTopIc.append(upLoadTopIcDiv);
        }
    }
</script>
</html>
